package com.ptu.controller.user;


import com.ptu.server.CartServer;
import com.ptu.util.Result;
import com.ptu.vo.CartVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "购物车模块")
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartServer cartServer;

    @Operation(summary = "添加商品到购物车")
    @PostMapping("/add")
    public Result<Void> addCart(Integer productId) {

        System.out.println("添加商品到购物车" + productId);
        cartServer.addCart(productId);
        return Result.success();
    }

    // 获取购物车列表
    @Operation(summary = "获取购物车列表")
    @PostMapping("/list")
    public Result<List> getCartList() {
        System.out.println("获取购物车列表");
        List<CartVO> cartList = cartServer.getCartList();
        System.out.println(cartList);
        return Result.success(cartList);
    }

    @Operation(summary = "删除购物车商品")
    @PostMapping("/delete")
    public Result<Void> deleteCart(Integer productId) {
        System.out.println("删除购物车商品" + productId);
        cartServer.deleteCart(productId);
        System.out.println("删除购物车成功");
        return Result.success();
    }

    @Operation(summary = "清空购物车")
    @PostMapping("/clear")
    public Result<Void> clearCart() {
        cartServer.clearCart();
        return Result.success();
    }

    //数量减一
    @Operation(summary = "数量减一")
    @PostMapping("/subtract")
    public Result<Void> subtractQuantity(Integer productId) {
        //log.info("productId:{}", productId);
        System.out.println("数量减一" + productId);
        cartServer.subtractQuantity(productId);
        return Result.success();
    }

    @Operation(summary = "批量删除")
    @PostMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestBody List<Integer> Ids) {
        System.out.println("批量删除");
        System.out.println(Ids);
        cartServer.batchDelete(Ids);
        System.out.println("批量删除成功");
        return Result.success();
    }


}
